/* eslint-disable no-array-constructor */
/* eslint-disable no-return-assign */
// 计算发布的时间距今
export function dealDate(datest) {
    var result = null
    var minute = 1000 * 60
    var hour = minute * 60
    var day = hour * 24
    // var halfamonth = day * 15;
    var month = day * 30
    var now = new Date().getTime()
    // var diffValue = now -getDateTimeStamp(datest) ;
    var diffValue = now - datest
    if (diffValue < 0) { return }
    var monthC = diffValue / month
    var weekC = diffValue / (7 * day)
    var dayC = diffValue / day
    var hourC = diffValue / hour
    var minC = diffValue / minute
    if (monthC >= 1) {
        return result = '' + parseInt(monthC) + '月前'
    } else if (weekC >= 1) {
        return result = '' + parseInt(weekC) + '周前'
    } else if (dayC >= 1) {
        return result = '' + parseInt(dayC) + '天前'
    } else if (hourC >= 1) {
        return result = '' + parseInt(hourC) + '小时前'
    } else if (minC >= 1) {
        return result = '' + parseInt(minC) + '分钟前'
    } else {
        result = '刚刚'
    }
    return result
}
// 计算发布时间距今时长
export function dealTime(datest) {
    var result = null
/*    var minute = 1000 * 60
    var hour = minute * 60
    var day = hour * 24
    var month = day * 30*/
    var now = new Date().getTime()
    var diffValue = (now - datest)/1000/60/60/24
    if (diffValue < 0) { return }
    var hourC = (now - datest)/1000/60/60%24
    return result = '' + parseInt(diffValue) + '天' + '' + parseInt(hourC) + '小时'
}

// （时间轴滞留时长）
export function holdUp(olddate,newdate) {
    var result = null
    var vDay = null
    var hourC = null
    var minC = null
    var now = new Date().getTime()

    if(newdate){
        vDay = (newdate - olddate)/1000/60/60/24
        hourC = (newdate - olddate)/1000/60/60%24
        minC = (newdate - olddate)/1000/60
    }else{
        now = new Date().getTime()
        vDay = (now - olddate)/1000/60/60/24
        hourC = (now - olddate)/1000/60/60%24
        minC = (now - olddate)/1000/60
    }
    if (vDay < 0) {
        return
    }else if (hourC < 1){
        return result = '' + parseInt(minC) + '分钟'
    }else if (vDay < 1){
        return result = '' + parseInt(hourC) + '小时'
    } else {
        return result = '' + parseInt(vDay) + '天' + '' + parseInt(hourC) + '小时'
    }
}


// 转换标准时间为时间戳：(处理带斜杠的2018/10/27 15:07:29) 或者可以直接进行转换new Date(“2018/10/27 15:07:29").getTime()
export function getDateTimeStampByXieGang(dateStr) {
    return Date.parse(dateStr.replace(/-/gi, '/'))
}
// 转换标准时间为时间戳
export function getDateTimeStampByDate(dateStr) {
    if ((dateStr + '').includes('-')) {
        return new Date(dateStr.replace(/-/g, '/')).getTime()
    } else {
        return new Date(dateStr).getTime()
    }
}
export function getDateTimeStampHourByDate(dateStr) {
    return new Date(new Date(dateStr).setHours(0, 0, 0, 0)).getTime()
}
// 计算星期
export function getWeek(dates) {
    var weeks = new Array('日', '一', '二', '三', '四', '五', '六')
    return '星期' + weeks[dates.getDay()]
}

// 时间错转标准时间
export function formatDate(date, fmt) {
    if ((date + '').includes('-')) {
        date = new Date(date.replace(/-/g, '/'))
    } else {
        date = new Date(date)
    }
    const o = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        'h+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        'S': date.getMilliseconds() // 毫秒
    }
    if (/(y+)/.test(fmt)) { // 年份
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (const k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
            const str = o[k] + ''
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
        }
    }
    return fmt
}

function padLeftZero(str) {
    return ('00' + str).substr(str.length)
}
// (formatDate(date, 'yyyy-MM-dd hh:mm'))
